package yxy.game.pm2.bean.area;

import org.redisson.api.RBucket;

import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;

/** 公共信息 */
public class CommonBean {
	// bean===========================================================================
	private String notice;
	private int clusterId; //专服id
	private int showQQ; //显示联系QQ
	private int showHaoPing; //显示好评图标
	private int showJiaQun; //显示加群图标

	public String getNotice() {
		return notice;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public int getClusterId() {
		return clusterId;
	}

	public void setClusterId(int clusterId) {
		this.clusterId = clusterId;
	}

	public int getShowQQ() {
		return showQQ;
	}

	public void setShowQQ(int showQQ) {
		this.showQQ = showQQ;
	}

	public int getShowHaoPing() {
		return showHaoPing;
	}

	public void setShowHaoPing(int showHaoPing) {
		this.showHaoPing = showHaoPing;
	}

	public int getShowJiaQun() {
		return showJiaQun;
	}

	public void setShowJiaQun(int showJiaQun) {
		this.showJiaQun = showJiaQun;
	}

	// redis==========================================================================

	public void set() {
		try {
			RBucket<CommonBean> bucket = touch(getClusterId());
			bucket.set(this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public String key(int clusterId) {
		return String.format("COMMON:%s", clusterId);
	}

	static private <V> RBucket<V> touch(int clusterId) throws RedisException {
		RedisConnection redis = RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
		return RedisManager.client(redis).getBucket(key(clusterId));
	}

	static public CommonBean get(int clusterId) {
		try {
			RBucket<CommonBean> bucket = touch(clusterId);
			CommonBean common = bucket.get();
			if (common == null) {
				common = create(clusterId);
			}
			return common;
		} catch (RedisException e) {
			e.printStackTrace();
			return null;
		}
	}

	static public CommonBean create(int clusterId) throws RedisException {
		CommonBean common = new CommonBean();
		common.setClusterId(clusterId);
		common.setNotice("公告");
		common.set();
		return common;
	}
}
